package com.sskj.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sskj.api.domain.ProjectMember;
import com.sskj.api.enums.ProjectMemberRoleTypeEnum;
import com.sskj.api.exception.LogicException;
import com.sskj.api.mapper.ProjectMapper;
import com.sskj.api.mapper.ProjectMemberMapper;
import com.sskj.api.service.IProjectMemberService;
import com.sskj.api.vo.req.ProjectMemberPageListReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Objects;

@Slf4j
@Service
public class ProjectMemberService extends ServiceImpl<ProjectMemberMapper, ProjectMember> implements IProjectMemberService {

    @Override
    public void add(ProjectMember projectMember) {
        LambdaQueryWrapper<ProjectMember> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProjectMember::getUserId, projectMember.getUserId())
                .eq(ProjectMember::getProjectId, projectMember.getProjectId());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicException("项目下面用户重复");
        }

        if (Objects.equals(projectMember.getRoleType(), ProjectMemberRoleTypeEnum.ADMIN.getRoleType())) {
            lambdaQueryWrapper.clear();
            lambdaQueryWrapper.eq(ProjectMember::getProjectId, projectMember.getProjectId())
                    .eq(ProjectMember::getRoleType, ProjectMemberRoleTypeEnum.ADMIN.getRoleType());
            if (count(lambdaQueryWrapper) > 0) {
                throw new LogicException("项目已绑定管理员角色");
            }
        }

        projectMember.setCreateTime(LocalDateTime.now());
        save(projectMember);
    }

    @Override
    public void deleteById(Long id) {
        ProjectMember projectMember = getById(id);
        if (projectMember != null && Objects.equals(projectMember.getRoleType(), ProjectMemberRoleTypeEnum.ADMIN.getRoleType())) {
            throw new LogicException("禁止删除管理员角色用户");
        }

        removeById(id);
    }

    @Override
    public void update(ProjectMember projectMember) {
        ProjectMember oldProjectMember = getById(projectMember.getId());
        if (oldProjectMember != null) {
            if (!Objects.equals(oldProjectMember.getRoleType(), ProjectMemberRoleTypeEnum.ADMIN.getRoleType()) && Objects.equals(projectMember.getRoleType(), ProjectMemberRoleTypeEnum.ADMIN.getRoleType())) {
                throw new LogicException("禁止更新为管理员权限");
            }

            projectMember.setUpdateTime(LocalDateTime.now());
            updateById(projectMember);
        }

    }

    @Override
    public IPage<ProjectMember> pageList(ProjectMemberPageListReq req) {
        IPage<ProjectMember> pageConfig = new Page<>(req.getPageNum(), req.getPageSize());
        return baseMapper.pageList(pageConfig, req);
    }

    @Override
    public void updateAdminRole(Long projectId, Long adminUserId) {
        LambdaQueryWrapper<ProjectMember> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProjectMember::getRoleType, ProjectMemberRoleTypeEnum.ADMIN.getRoleType())
                .eq(ProjectMember::getProjectId, projectId);
        remove(lambdaQueryWrapper);

        lambdaQueryWrapper.clear();
        lambdaQueryWrapper.eq(ProjectMember::getProjectId, projectId)
                .eq(ProjectMember::getUserId, adminUserId);
        ProjectMember one = getOne(lambdaQueryWrapper);
        if (one != null) {
            one.setRoleType(ProjectMemberRoleTypeEnum.ADMIN.getRoleType());
            one.setUpdateTime(LocalDateTime.now());
            updateById(one);
        } else {
            ProjectMember projectMember = new ProjectMember();
            projectMember.setProjectId(projectId);
            projectMember.setUserId(adminUserId);
            projectMember.setRoleType(ProjectMemberRoleTypeEnum.ADMIN.getRoleType());
            projectMember.setCreateTime(LocalDateTime.now());

            save(projectMember);
        }
    }

    @Override
    public void deleteByProjectId(Long projectId) {
        LambdaQueryWrapper<ProjectMember> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProjectMember::getProjectId, projectId);
        remove(lambdaQueryWrapper);
    }

    @Override
    public Long countProjects(Long userId) {
        LambdaQueryWrapper<ProjectMember> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProjectMember::getUserId, userId);
        return count(lambdaQueryWrapper);
    }
}
